/*
 * @Author: szx
 * @Date: 2022-03-19 12:45:49
 * @LastEditTime: 2022-03-19 13:05:12
 * @Description:
 * @FilePath: \leetcode\剑指offer\16\16.js
 */
/**
 * @param {number} x
 * @param {number} n
 * @return {number}
 */
var myPow = function (x, n) {
    const dfs = (x, n) => {
        if (n == 0) return 1;
        const num = dfs(x, Math.floor(n / 2));
        if (n & 1) {
            return num * num * x;
        } else {
            return num * num;
        }
    };

    return n > 1 ? dfs(x, n) : 1 / dfs(x, n);
};

var myPow = function (x, n) {
    function quickMul(x, n) {
        let res = 1;
        while (n > 0) {
            if (n & 1) res *= x;
            x *= x;
            n = Math.floor(n / 2);
        }
        return res;
    }
    return n >= 0 ? quickMul(x, n) : 1 / quickMul(x, -n);
};
myPow();
